#include "functionality.h"

/*  Int to string convertion
 *  @param1 [val] = value to store as a string
 *  @param2 [base] =
 *  @return = string representation of integer value
 * */
char* itoa(int val, int base)
    {
    static char buf[32]={0};
    int i=30;
    if ( val == 0 )
        {
        return "0";
        }
    for(; val && i ; --i, val /= base)
        {
        buf[i]="0123456789abcdef"[val%base];
        }
    return &buf[i+1];
    }

/*  Data serialization: integer array->string; column as a delimeter
 *  @param1 [data] = integer array to be serialized into a string
 *  @return = string representation of integer array
 * */
void serialize(int* data, char* target)
    {
    int i;
    char retVal[MESSAGE_STR_SIZE];
    clearArray(retVal); //Initialize the array to be empty
    for (i = 0 ; i < MESSAGE_INT_SIZE; i++)
        {
        strcat(retVal,itoa(data[i],10));
        if(i!=(MESSAGE_INT_SIZE-1))
            {
            strcat(retVal,":");
            }
        else
            {
            strcat(retVal,"\n");
            }
        }
    strcpy(target,retVal);
    }

/*  Data deserialization: string->integer array; column as a delimeter
 *  @param1 [data] = message string to be deserialized into an array of integers
 *  @return = string representation of integer array
 * */
void deserialize(char* data, int* target)
    {
    int i;
    int retValCounter; //integer array cursor
    int bufferCounter; //buffer cursor
    char buffer[6]; //to hold the data under processing
    retValCounter = 0;
    bufferCounter = 0;
    for(i=0;i<MESSAGE_STR_SIZE;i++)
        {
        if(data[i]=='\n')//end of message
            {
            target[retValCounter] = atoi(buffer);
            break;
            }
        else if(data[i]==':')//next pattern
            {
            target[retValCounter] = atoi(buffer);
            retValCounter++;
            bufferCounter = 0;
            clearArray(buffer);
            }
        else
            {
            target[retValCounter] = atoi(buffer);
            buffer[bufferCounter] = data[i];
            bufferCounter++;
            }
        }
    }

/*  Data presentation: show as a table the array of integers(additional info - reserved tickets)
 *  @param1 [all] = data to be show as a table
 *  @param2 [reserved] = data to be show as a table
 * */
void showTickets(int* all, int* reserved)
    {
    printf("Tickets' status\n");
    printf("Category:  A \t B \t C \t D \t E \t\n");
    printf("Available: %d \t %d \t %d \t %d \t %d \t\n", all[1], all[2], all[3], all[4], all[5]);
    printf("Reserved:  %d \t %d \t %d \t %d \t %d \t\n", reserved[1], reserved[2], reserved[3], reserved[4], reserved[5]);
    }

/*  Clear the array: init all elements to be null
 *  @param1 [array] = array to be cleared
 *  @param2 [size] = array's size
 * */
void clearVoidArray(void* array, int size)
    {
    memset(array, 0, size);
    }

/*  Copy one array into another: safe copy
 *  @param1 [array] = array to be copied
 *  @param2 [size] = array's size
 *  @param3 [array] = target array
 *  @param4 [size] = target array's size
 * */
void copyVoidArray(void* from, int sizeOfFrom, void* to, int sizeOfTo)
    {
    if(sizeOfFrom<=sizeOfTo)
        {
        memcpy(to, from, sizeOfFrom);
        }
    else
        {
        memcpy(to, from, sizeOfTo);
        }
    }

void clearScr()
    {
    printf("\033[2J");
    printf("\033[0;0f");
    }

/* Main function for serialization testing */
/*
int main()
    {
    int s[6];
    int i;
    char test[MESSAGE_STR_SIZE];
    //serialization test
    printf("SERIALIZATION\n");
    s[0]=12; s[1]=2; s[2]=214; s[3]=5; s[4]=456; s[5]=628;
    serialize(s, test);
    printf("S:%s", test);
    //deserialization test
    printf("DESERIALIZATION\n");
    strcpy(test,"23:56:235:5678:234:94");
    deserialize(test, s);
    for(i=0;i<MESSAGE_INT_SIZE;i++) { printf("S:%d\n",s[i]); }
    return 0;
    }
*/



